<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input v-model="queryParams.orderId" placeholder="请输入订单号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户id" prop="uid">
        <el-input v-model="queryParams.uid" placeholder="请输入用户id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入姓名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户电话" prop="userPhone">
        <el-input v-model="queryParams.userPhone" placeholder="请输入用户电话" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="详细地址" prop="userAddress">
        <el-input v-model="queryParams.userAddress" placeholder="请输入详细地址" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="商品id" prop="pid">
        <el-input v-model="queryParams.pid" placeholder="请输入商品id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="商品" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入商品" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订单商品总数" prop="totalNum">
        <el-input v-model="queryParams.totalNum" placeholder="请输入订单商品总数" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订单总价" prop="totalPrice">
        <el-input v-model="queryParams.totalPrice" placeholder="请输入订单总价" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="实际支付金额" prop="payPrice">
        <el-input v-model="queryParams.payPrice" placeholder="请输入实际支付金额" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="运费金额" prop="freightPrice">
        <el-input v-model="queryParams.freightPrice" placeholder="请输入运费金额" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="抵扣金额" prop="deductionPrice">
        <el-input v-model="queryParams.deductionPrice" placeholder="请输入抵扣金额" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker clearable v-model="queryParams.payTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退款时间" prop="refundReasonTime">
        <el-date-picker clearable v-model="queryParams.refundReasonTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择退款时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退款原因" prop="refundReasonWap">
        <el-input v-model="queryParams.refundReasonWap" placeholder="请输入退款原因" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundPrice">
        <el-input v-model="queryParams.refundPrice" placeholder="请输入退款金额" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="不退款的理由" prop="refundReason">
        <el-input v-model="queryParams.refundReason" placeholder="请输入不退款的理由" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="快递名称/送货人姓名" prop="deliveryName">
        <el-input v-model="queryParams.deliveryName" placeholder="请输入快递名称/送货人姓名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="快递单号/手机号" prop="deliveryId">
        <el-input v-model="queryParams.deliveryId" placeholder="请输入快递单号/手机号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="是否删除" prop="isDel">
        <el-input v-model="queryParams.isDel" placeholder="请输入是否删除" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="成本价" prop="cost">
        <el-input v-model="queryParams.cost" placeholder="请输入成本价" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="支付渠道" prop="channel">
        <el-input v-model="queryParams.channel" placeholder="请输入支付渠道" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="后台是否删除" prop="isSystemDel">
        <el-input v-model="queryParams.isSystemDel" placeholder="请输入后台是否删除" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺id" prop="storeId">
        <el-input v-model="queryParams.storeId" placeholder="请输入店铺id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="核销时间" prop="verificationTime">
        <el-date-picker clearable v-model="queryParams.verificationTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择核销时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="商品上级分享码" prop="share">
        <el-input v-model="queryParams.share" placeholder="请输入商品上级分享码" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['ds:order:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ds:order:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ds:order:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['ds:order:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单ID" align="center" prop="id" />
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="用户id" align="center" prop="uid" />
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column label="用户电话" align="center" prop="userPhone" />
      <el-table-column label="商品id" align="center" prop="pid" />
      <el-table-column label="商品" align="center" prop="productName" />
      <el-table-column label="订单商品总数" align="center" prop="totalNum" />
      <el-table-column label="订单总价" align="center" prop="totalPrice" />
      <el-table-column label="实际支付金额" align="center" prop="payPrice" />
      <el-table-column label="抵扣金额" align="center" prop="deductionPrice" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.payTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType" />
      <el-table-column label="订单状态" align="center" prop="status" />
      <el-table-column label="成本价" align="center" prop="cost" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="" align="center" prop="productImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.productImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="店铺id" align="center" prop="storeId" />
      <el-table-column label="核销时间" align="center" prop="verificationTime" width="180">
        <template #default="scope">
          <span>{{
            parseTime(scope.row.verificationTime, "{y}-{m}-{d}")
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品上级分享码" align="center" prop="share" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ds:order:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ds:order:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="用户id" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="用户电话" prop="userPhone">
          <el-input v-model="form.userPhone" placeholder="请输入用户电话" />
        </el-form-item>
        <el-form-item label="详细地址" prop="userAddress">
          <el-input v-model="form.userAddress" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="商品id" prop="pid">
          <el-input v-model="form.pid" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="商品" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品" />
        </el-form-item>
        <el-form-item label="订单商品总数" prop="totalNum">
          <el-input v-model="form.totalNum" placeholder="请输入订单商品总数" />
        </el-form-item>
        <el-form-item label="订单总价" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入订单总价" />
        </el-form-item>
        <el-form-item label="邮费" prop="totalPostage">
          <el-input v-model="form.totalPostage" placeholder="请输入邮费" />
        </el-form-item>
        <el-form-item label="实际支付金额" prop="payPrice">
          <el-input v-model="form.payPrice" placeholder="请输入实际支付金额" />
        </el-form-item>
        <el-form-item label="支付邮费" prop="payPostage">
          <el-input v-model="form.payPostage" placeholder="请输入支付邮费" />
        </el-form-item>
        <el-form-item label="运费金额" prop="freightPrice">
          <el-input v-model="form.freightPrice" placeholder="请输入运费金额" />
        </el-form-item>
        <el-form-item label="抵扣金额" prop="deductionPrice">
          <el-input v-model="form.deductionPrice" placeholder="请输入抵扣金额" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable v-model="form.payTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款时间" prop="refundReasonTime">
          <el-date-picker clearable v-model="form.refundReasonTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择退款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款原因" prop="refundReasonWap">
          <el-input v-model="form.refundReasonWap" placeholder="请输入退款原因" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundPrice">
          <el-input v-model="form.refundPrice" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="不退款的理由" prop="refundReason">
          <el-input v-model="form.refundReason" placeholder="请输入不退款的理由" />
        </el-form-item>
        <el-form-item label="快递名称/送货人姓名" prop="deliveryName">
          <el-input v-model="form.deliveryName" placeholder="请输入快递名称/送货人姓名" />
        </el-form-item>
        <el-form-item label="快递单号/手机号" prop="deliveryId">
          <el-input v-model="form.deliveryId" placeholder="请输入快递单号/手机号" />
        </el-form-item>
        <el-form-item label="是否删除" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入是否删除" />
        </el-form-item>
        <el-form-item label="成本价" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入成本价" />
        </el-form-item>
        <el-form-item label="支付渠道" prop="channel">
          <el-input v-model="form.channel" placeholder="请输入支付渠道" />
        </el-form-item>
        <el-form-item label="后台是否删除" prop="isSystemDel">
          <el-input v-model="form.isSystemDel" placeholder="请输入后台是否删除" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="" prop="productImage">
          <image-upload v-model="form.productImage" />
        </el-form-item>
        <el-form-item label="店铺id" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入店铺id" />
        </el-form-item>
        <el-form-item label="核销时间" prop="verificationTime">
          <el-date-picker clearable v-model="form.verificationTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择核销时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品上级分享码" prop="share">
          <el-input v-model="form.share" placeholder="请输入商品上级分享码" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
} from "@/api/ds/order";

const { proxy } = getCurrentInstance();

const orderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    uid: null,
    realName: null,
    userPhone: null,
    userAddress: null,
    pid: null,
    productName: null,
    totalNum: null,
    totalPrice: null,
    totalPostage: null,
    payPrice: null,
    payPostage: null,
    freightPrice: null,
    deductionPrice: null,
    payTime: null,
    payType: null,
    status: null,
    refundStatus: null,
    refundReasonTime: null,
    refundReasonWap: null,
    refundPrice: null,
    refundReason: null,
    deliveryName: null,
    deliveryType: null,
    deliveryId: null,
    isDel: null,
    cost: null,
    shippingType: null,
    channel: null,
    isSystemDel: null,
    productImage: null,
    storeId: null,
    verificationTime: null,
    share: null,
  },
  rules: {
    uid: [{ required: true, message: "用户id不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询订单列表 */
function getList() {
  loading.value = true;
  listOrder(queryParams.value).then((response) => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderId: null,
    uid: null,
    realName: null,
    userPhone: null,
    userAddress: null,
    pid: null,
    productName: null,
    totalNum: null,
    totalPrice: null,
    totalPostage: null,
    payPrice: null,
    payPostage: null,
    freightPrice: null,
    deductionPrice: null,
    payTime: null,
    payType: null,
    status: null,
    refundStatus: null,
    refundReasonTime: null,
    refundReasonWap: null,
    refundPrice: null,
    refundReason: null,
    deliveryName: null,
    deliveryType: null,
    deliveryId: null,
    isDel: null,
    cost: null,
    shippingType: null,
    channel: null,
    isSystemDel: null,
    remark: null,
    createTime: null,
    productImage: null,
    storeId: null,
    verificationTime: null,
    share: null,
  };
  proxy.resetForm("orderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getOrder(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改订单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateOrder(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrder(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除订单编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delOrder(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ds/order/export",
    {
      ...queryParams.value,
    },
    `order_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
